# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
import random
import time
from requests.auth import HTTPProxyAuth


def google_search(query, num_results=10, retries=3):
    """
    使用 Google 搜索并通过代理进行爬取。

    :param query: 搜索的关键词
    :param num_results: 返回的搜索结果数量
    :param retries: 请求失败的重试次数
    :return: 搜索结果的列表
    """
    base_url = "https://www.google.com/search"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
    }
    params = {"q": query, "num": num_results}

    proxy = "http://193.254.16.26:63034"
    proxies = {
        "http": proxy,
        "https": proxy
    }
    auth = HTTPProxyAuth('mSDz7FD3', 'DnzXXg5c')

    for attempt in range(retries):
        try:
            print(f"Using proxy: {proxy}")

            response = requests.get(base_url, headers=headers, proxies=proxies, auth=auth, params=params, timeout=10)
            response.raise_for_status()

            # 使用 BeautifulSoup 解析 HTML
            soup = BeautifulSoup(response.text, "html.parser")
            results = []

            for item in soup.select('div.tF2Cxc'):
                title = item.select_one('h3').get_text()
                link = item.select_one('a')['href']
                description = item.select_one('.VwiC3b').get_text()
                results.append({
                    "title": title,
                    "link": link,
                    "description": description
                })

            return results

        except requests.exceptions.RequestException as e:
            print(f"Request failed (attempt {attempt + 1}): {e}")
            time.sleep(2)  # 延迟 2 秒后重试

    print("All retry attempts failed.")
    return []


# 使用示例
search_results = google_search("Python web scraping", num_results=5)
for result in search_results:
    print(f"Title: {result['title']}")
    print(f"Link: {result['link']}")
    print(f"Description: {result['description']}")
    print()
